#!/bin/bash

OUT_FILE=results_`date +%Y%m%d%H%M`.csv

NUM_MAT=6
MAT_DIM=(32 128 256 512 1024 2048)

NUM_ELEM=5
ELEM_DIM=(32 64 128 256 512)

NUM_ITER=5

cat header > $OUT_FILE

for (( i = 0 ; $i < NUM_MAT ; i = $i + 1 )) ; do
	for (( j = 0 ; $j < NUM_ELEM ; j = $j + 1 )) ; do
		if (( ${ELEM_DIM[$j]} <= ${MAT_DIM[$i]} )) ; then
			MDIM=${MAT_DIM[$i]}
			EDIM=${ELEM_DIM[$j]}
			BDIM=$(($MDIM / $EDIM))
			for (( k = 0 ; $k < NUM_ITER ; k = $k + 1 )) ; do

echo "`date`: $(($i*$NUM_ELEM*$NUM_ITER + $j*NUM_ITER + $k))/$(($NUM_MAT*$NUM_ELEM*$NUM_ITER)) $BDIM $EDIM $NUM_ITER"

				./htprefetch create $BDIM $EDIM random -out m1 -d > /dev/null
				./htprefetch create $BDIM $EDIM random -out m2 -d > /dev/null
				./htprefetch add -in m1 -in m2 -out m3 >> $OUT_FILE
				
				./htprefetch create $BDIM $EDIM random -out m1 -d > /dev/null
				./htprefetch create $BDIM $EDIM random -out m2 -d > /dev/null
				./htprefetch add -p -in m1 -in m2 -out m3 >> $OUT_FILE

				./htprefetch create $BDIM $EDIM random -out m1 > /dev/null
				./htprefetch create $BDIM $EDIM random -out m2 > /dev/null
				./htprefetch add -w -in m1 -in m2 -out m3 >> $OUT_FILE
				
				./htprefetch create $BDIM $EDIM random -out m1 > /dev/null
				./htprefetch create $BDIM $EDIM random -out m2 > /dev/null
				./htprefetch add -w -p -in m1 -in m2 -out m3 >> $OUT_FILE

				./htprefetch create $BDIM $EDIM random -out m1 -d > /dev/null
				./htprefetch create $BDIM $EDIM random -out m2 -d > /dev/null
				./htprefetch add -in m1 -d -in m2 -d -out m3 >> $OUT_FILE

				./htprefetch create $BDIM $EDIM random -out m1 -d > /dev/null
				./htprefetch create $BDIM $EDIM random -out m2 -d > /dev/null
				./htprefetch add -p -in m1 -d -in m2 -d -out m3 >> $OUT_FILE

				./htprefetch create $BDIM $EDIM random -out m1 -d > /dev/null
				./htprefetch create $BDIM $EDIM random -out m2 -d > /dev/null
				./htprefetch add -in m1 -c -d -in m2 -c -d -out m3 >> $OUT_FILE
				
				./htprefetch create $BDIM $EDIM random -out m1 -d > /dev/null
				./htprefetch create $BDIM $EDIM random -out m2 -d > /dev/null
				./htprefetch add -p -in m1 -c -d -in m2 -c -d -out m3 >> $OUT_FILE

				./htprefetch create $BDIM $EDIM random -out m1 -d > /dev/null
				./htprefetch create $BDIM $EDIM random -out m2 -d > /dev/null
				./htprefetch multiply -in m1 -in m2 -out m3 >> $OUT_FILE

				./htprefetch create $BDIM $EDIM random -out m1 -d > /dev/null
				./htprefetch create $BDIM $EDIM random -out m2 -d > /dev/null
				./htprefetch multiply -p -in m1 -in m2 -out m3 >> $OUT_FILE

				./htprefetch create $BDIM $EDIM random -out m1 > /dev/null
				./htprefetch create $BDIM $EDIM random -out m2 > /dev/null
				./htprefetch multiply -w -in m1 -in m2 -out m3 >> $OUT_FILE

				./htprefetch create $BDIM $EDIM random -out m1 > /dev/null
				./htprefetch create $BDIM $EDIM random -out m2 > /dev/null
				./htprefetch multiply -w -p -in m1 -in m2 -out m3 >> $OUT_FILE

				./htprefetch create $BDIM $EDIM random -out m1 -d > /dev/null
				./htprefetch create $BDIM $EDIM random -out m2 -d > /dev/null
				./htprefetch multiply -in m1 -d -in m2 -d -out m3 >> $OUT_FILE

				./htprefetch create $BDIM $EDIM random -out m1 -d > /dev/null
				./htprefetch create $BDIM $EDIM random -out m2 -d > /dev/null
				./htprefetch multiply -p -in m1 -d -in m2 -d -out m3 >> $OUT_FILE

				./htprefetch create $BDIM $EDIM random -out m1 -d > /dev/null
				./htprefetch create $BDIM $EDIM random -out m2 -d > /dev/null
				./htprefetch multiply -in m1 -c -d -in m2 -c -d -out m3 >> $OUT_FILE

				./htprefetch create $BDIM $EDIM random -out m1 -d > /dev/null
				./htprefetch create $BDIM $EDIM random -out m2 -d > /dev/null				
				./htprefetch multiply -p -in m1 -c -d -in m2 -c -d -out m3 >> $OUT_FILE
			done;
		fi
	done;
done;